package org.nism.jrebel.controller;

import org.nism.jrebel.utils.EmailUtil;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Produces;
import org.noear.solon.boot.web.ContextBase;
import org.noear.solon.boot.web.MimeType;

import java.util.Calendar;
import java.util.UUID;

/**
 * IndexController
 *
 * @author inism
 */
@Controller
public class IndexController {

    private static final String FAVICON = "";
    private static final String EG_IMG = "";
    private static final String FAVICON_LINK = "<link rel='icon' href='" + FAVICON + "'>";

    private static final String HTML_CSS = "<style>h1{text-align:center}p{text-align:center}.b{background-color:#21252b}.w{color:white}.r{color:red}.n{margin:0;padding:0}.f{position:fixed;text-align:center;line-height:50px;width:100%;bottom:0;}.p2{padding:10px}</style>";
    private static final String HTML_JS = "<script>function fn(e){var i=document.createElement('input');document.body.appendChild(i);i.setAttribute('value',e.innerText);i.select();if(document.execCommand('copy')){alert(e.innerText+'\\n copied!')}document.body.removeChild(i);location.reload()}</script>";

    @Get
    @Mapping("/")
    @Produces(MimeType.TEXT_HTML_VALUE)
    public String index(ContextBase ctx) {

        return "<!DOCTYPE html><html lang='zh'>" +
                "<head>" + "<title>JRebel Server</title>" + FAVICON_LINK + HTML_CSS + "</head>" +
                "<body class='n'>" +
                "<h1 class='b w n p2'><img style='margin-top: 7px' src='" + FAVICON + "'> JRebel & XRebel License Server!</h1>" +
                "<p><img style='margin-top: 7px' src='" + EG_IMG + "'></p>" +
                "<p>URL: 👉 <a href='javascript:void(0)' onclick='fn(this)' title='copy and refresh!'>" + ctx.uri() + UUID.randomUUID() + "</a> 👈</p>" +
                "<p>email: 👉 <a href='javascript:void(0)' onclick='fn(this)' title='copy and refresh!'>" + EmailUtil.random() + "</a> 👈</p>" +
                "<div class='b w f'>&copy;2022-" + Calendar.getInstance().get(Calendar.YEAR) + "</div>" +
                "</body>" + HTML_JS + "</html>";
    }
}
